//
//  ChangeNicknameVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/3.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ChangeNicknameVC.h"
#import "UserService.h"
#import "UIimage+Extends.h"
#import "UIButton+Gradient.h"

@interface ChangeNicknameVC ()

@property(nonatomic,weak) IBOutlet UITextField *userNameFid;
@property(nonatomic,weak) IBOutlet UIButton *subBtn;
@property(nonatomic,strong) UserService  *services;
@end

@implementation ChangeNicknameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"昵称"];
    self.subBtn.layer.masksToBounds = YES;
    self.subBtn.layer.cornerRadius = 5;
    
    if(self.youkeModel){
        self.userNameFid.text = self.youkeModel.nickname;
    }
    
    [self.userNameFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self changedTextField:nil];
}

-(UserService*)services{
    if (_services == nil) {
        _services = [[UserService alloc] init];
    }
    return _services;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.nickNameStr) {
        self.userNameFid.text = self.nickNameStr;
    }
}

-(void)changedTextField:(UITextField*)fid{
    if (self.userNameFid.text.length<1) {
        [self.subBtn setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.subBtn.bounds.size] forState:UIControlStateNormal];
    }else{
        [self.subBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }
}

-(IBAction)submitUserNameAction:(id)sender{
     
    if (self.userNameFid.text.length == 0) {
        [WHToast showMessage:@"请输入昵称" duration:1.5 finishHandler:nil];
        return;
    }
    
    TYMWeak(self);
    if (self.youkeModel) {
        [self.services getUpdateyoukeUserUserId:self.youkeModel.userId familyId:[UserTool getCurrentFamily].familyId nickname:self.userNameFid.text headImgUrl:self.youkeModel.headImgUrl completion:^(LMJBaseResponse * _Nonnull response) {
            if (response.code == 0) {
                if (weakself.changNameBlock) {
                    weakself.changNameBlock(self.userNameFid.text);
                }
                [WHToast showMessage:@"修改成功" duration:2 finishHandler:^{
                    [weakself.navigationController popViewControllerAnimated:YES];
                }];
            }else{
                [WHToast showMessage:response.message duration:2 finishHandler:nil];
            }
        }];
    }else{
        [self.services getUpdateUser:[UserTool getLogInUser].realName sex:[UserTool getLogInUser].sex nickName:self.userNameFid.text headImagUrl:[UserTool getLogInUser].headImgUrl completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
            if (response.code == 0) {
                [UserTool getLogInUser].nickname = weakself.userNameFid.text;
                [WHToast showMessage:@"修改成功" duration:2 finishHandler:^{
                    [weakself.navigationController popViewControllerAnimated:YES];
                }];
            }else{
                [WHToast showMessage:response.message duration:2 finishHandler:nil];
            }
        }];
        
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *blank = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
    if(![string isEqualToString:blank]) {
        return NO;
    }
    if (textField.text.length >= 8 && ![string isEqualToString:@""]) {
        return NO;
    }
    return YES;
}
@end
